package jdklearning.bit;


import java.util.Random;

public class BitOperation {

	private static void printNum(int n){
	    String num = Integer.toBinaryString(n);
	    if(num.length() == 32){
	        System.out.println(num);
	    }else{
	        StringBuilder sb = new StringBuilder("");
	        for(int i =0;i < 32 - num.length(); i ++){
	        sb.append("0");
	        }
	        System.out.println(sb.toString() + num);
	    }
	}
	
	
	private static void oppsite(){
		//1、~操作符（取反），对应二进制位取反，0变成1，1变成0
		int num = 3;
		printNum(num);
		printNum(~num);
	}
	
	
	private static void and() {
		//2、&操作符（与），对应二进制位进行与操作，都为1时变成1，其他变为0
		printNum((31-1) & 5);
		System.out.println((31-1) & 5);
	}
	
	private static void or() {
		//3、|操作符（或）对应二进制位进行或操作，都为0时变成0，其他变为1
		int num1 = 4;
		int num2 = 7;
		printNum(num1);
		printNum(num2);
		printNum(num1 | num2);
	}
	
	private static void xor() {
		//4、^操作符（异或），对应二进制位相同时，该位变成0，否则变成1
		int num11 = 5;
		int num22 = 9;
		printNum(num11);
		printNum(num22);
		printNum(num11 ^ num22);
	}


	/**
	 * 移位,最高位-1表示负数
	 */
	public static void leftRightBitMove(){
		long l1 = 1L << 32;
		long l2 = 1L << 31;
		System.out.println(l1);
		System.out.println(l2);
		int i1 = 1 << 32;
		int i2 = 1 << 31;
		System.out.println(i1);
		System.out.println(i2);
		System.out.println(Integer.MIN_VALUE);
		System.out.println(Integer.MIN_VALUE & 1);
	}


	/**
	 *
	 * int型进制数值和字符串互相转换
	 * 进制数值转字符串
	 * 字符串转进制数
	 * 补零
	 */
	public static void bitValueAndString(int originValue){
		String binaryString = Integer.toBinaryString(originValue);
//		String binaryStringWithZeroPrefix = String.format("%032d", Integer.);
		Integer integer = Integer.valueOf(binaryString, 2);
		System.out.println(binaryString);
//		System.out.println(binaryStringWithZeroPrefix);
		System.out.println(integer);
	}



	/***
	 * 5、<<操作（左移），二进制位向左移动，右边填充0


int num1 = 5;
printNum(num1);
printNum(num1 << 2);
 结果如下：

00000000000000000000000000000101
00000000000000000000000000010100


6、>>操作（右移），二进制位向右移动，左边填充0


int num1 = 5;
printNum(num1);
printNum(num1 >> 2);
 结果如下：

00000000000000000000000000000101
00000000000000000000000000000001

位操作常见应用

1、不使用中间变量交换两个数

int num1 = 2;
int num2 = 5;
num1 = num1^num2;
num2 = num2^num1;
num1 = num1^num2;
System.out.println("num1:" + num1 +"\n"+ "num2:" + num2 );
 2、求2的N次方

//求2的32次方：
System.out.println(Math.pow(2, 32));
System.out.println(1L<<32);
 

3、判断奇数偶数

int num1 = 4;
int num2 = 9;
if(num1%2 == 0){
    System.out.println("偶数");
}else{
    System.out.println("奇数");
}
System.out.println((((int)num1&1) == 1) ? "奇数" : "偶数");
System.out.println((((int)num2&1) == 1) ? "奇数" : "偶数");
 4、求绝对值

int num = -3;
System.out.println(Math.abs(num));
int i = num >> 31; 
System.out.println(i == 0 ? num : (~num + 1));
	 */


	public static void testBit(){
		int n = 32; //2^n
		int i = new Random().nextInt(); //可能会返回负数
		int i2 = new Random().nextInt(10000);
		System.out.println(  i);
		System.out.println(  i2);
		System.out.println( (n - 1) & i2);
		System.out.println( i2 % n);


		System.out.println(5>>>6);
	}
	
	public static void main(String[] args) {
//		leftRightBitMove();
//		bitValueAndString(255);

	}
}
